<?php

$oUrl = &App::getUrl();
$oReq = &App::getRequest();
$oTpl = &App::getTemplate();
$oDb = &Database::get();
$oLang = &App::getLanguage();

$iPage = $oReq->get('page', 1);
$iPageSize = 20;

$iId = $oReq->get('id');

if ( $oReq->get('selected') )
{
	$aSelected = $oReq->getArray('deleteid');
	$sVarType = $oReq->get('vartype');
	if ( isset($aSelected[0]) && isset($sVarType) )
	{
		$oDb->update(App::getT('language_options'), array('selected' => ''), "selected != '' AND varname = '". $sVarType ."'");
		$oDb->update(App::getT('language_options'), array('selected' => '1'), "optionid = '". $aSelected[0] ."'");
	}

	Cache::subStrRemove('option_');

	App::gotoUrl(App::getPage(), array('varname' => $oReq->get('varname'), 'page' => $iPage), App::format('Mod_Language.default_option_set'), false);
}

if ( $oReq->get('delete') )
{
	$aDelete = $oReq->getArray('deleteid');

	foreach($aDelete as $iDelete)
	{
		if ( !empty($iDelete) )
		{
			$oDb->_delete(App::getT('language_options'), "optionid = '". $iDelete ."'");
		}
	}

	Cache::subStrRemove('option_');

	App::gotoUrl('admin.language.options', array('id' => $iId, 'page' => $iPage), App::format('language_options_deleted'), false);
}

if ( $oReq->get('save') )
{
	$aVal = $oReq->getArray('text');
	$aRemoveResult = array();
	foreach($aVal as $iNewId => $sNewText)
	{
		$aQuery = array
		(
			'text' => $sNewText
		);
		$oDb->update(App::getT('language_options'), $aQuery, "optionid = '". $iNewId ."'");
	}	
	
	$aRequests = &$oReq->getAll(array('save', 'text'));
	$aReturn = array();
	foreach($aRequests as $n => $v)
	{
		$aReturn[$n] = $v;
	}
	$oLang->removeCachedFiles();
	App::gotoUrl('admin.language.options', $aReturn, App::format('language_phrase_updated'), false);
}

if ( $oReq->get('default') )
{
	$aOptions = $oDb->getRows("SELECT `optionid`, `default` FROM ". App::getT('language_options') ."
		WHERE 1 ". ($iId ? "p.languageid = '". $oDb->escape($iId) ."'" : '') ."
	");

	foreach($aOptions as $aOption)
	{
		$oDb->update(App::getT('language_options'), array('text' => $aOption['default']), "optionid = '". $aOption['optionid'] ."'");
	}

	$oDb->query("OPTIMIZE TABLE `". App::getT('language_options') ."`");

	$oLang->removeCachedFiles();

	App::gotoUrl('admin.language.options', array(), App::format('language_reverted'), false);
}

$sQ = "";

if ( $iId )
{
	$sQ .= " AND p.languageid = '". $oDb->escape($iId) ."' ";
}

if ( $sQuery = $oReq->get('q') )
{
	$sType = $oReq->get('type');

	switch($sType)
	{
		case 'varname':
		$sQ .= " AND p.`varname` LIKE '%". $oDb->escape($sQuery) ."%' ";
		break;
		case 'text':
		default:
		$sQ .= "AND p.`text` LIKE '%". $oDb->escape($sQuery) ."%'";
		break;
		case 'both':
		$sQ .= " AND (p.`varname` LIKE '%". $oDb->escape($sQuery) ."%' OR p.`text` LIKE '%". $oDb->escape($sQuery) ."%') ";
		break;
	}
}

if ( $sOption = $oReq->get('varname') )
{
	$sQ .= " AND p.`varname` = '". $oDb->escape($sOption) ."' ";
}

$iCnt = $oDb->getField("SELECT COUNT(*) FROM ". App::getT('language_options') ." AS p
	WHERE 1 ". $sQ ."");

$iOffset = $oDb->_getPageNum($iPage, $iPageSize, $iCnt);

$aItems = $oDb->getRows("SELECT p.varid, p.`optionid`, p.`varname`, p.`text`, p.`default`, p.`selected`, l.`title` FROM
	". App::getT('language_options') ." AS p
	LEFT JOIN ". App::getT('language') ." AS l ON(p.languageid = l.languageid)
	WHERE 1 ". $sQ ."
	ORDER BY `optionid` DESC LIMIT ". $iOffset .",". $iPageSize ."");

$oPager = App::newObject('Pager', $iCnt, $iPage, $iPageSize);

$aRequests = &$oReq->getAll(array('page', 'save', 'deleteid', 'delete', 'search', 'selected', 'vartype'));
if ( count($aRequests) > 0 )
{
	foreach($aRequests as $n => $v)
	{
		$oUrl->setParam($n, $v);
	}
}

$iFrom = $oPager->getOffset($iPage, $iPageSize);
$iTo = (($iFrom + $iPageSize) > $iCnt) ? $iCnt : ($iFrom + $iPageSize);

$aOptions = $oDb->getRows("SELECT varname, languagevar FROM ". App::getT('product_options') ."");

$oTpl->assign(array
(
	'aOptions' => $aOptions,
	'iId' => $iId,
	'aItems' => $aItems,
	'aPaging'   => $oPager->getInfo($oUrl),
	'aLanguages' => $oLang->getLanguages(),
	'aModules' => App::getParam('modules'),
	'aPost' => $aRequests,
	'iCnt'             => $iCnt,
	'iTo'              => $iTo,
	'iFrom'            => ($iCnt) ? ($iFrom + 1) : 0,
));

?>